package com.itstyle.mytest.queue.blockingQueueTest;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author xianguang
 * @date 2019-12-31 15:57S
 */
public class MyBlockingQueue<E> {
    private Queue<E> queue;

    private int capacity;

    public MyBlockingQueue() {
        this(16);
    }

    public MyBlockingQueue(int capacity) {
        this.queue=new LinkedList<>();
        this.capacity = capacity;
    }

    public synchronized void put(E e) throws InterruptedException {

        while (capacity==queue.size()){
            this.wait();
        }
        if(0==queue.size()){
            this.notifyAll();
        }
        queue.add(e);
    }


    public synchronized E take() throws InterruptedException {
        while (0==queue.size()){
            this.wait();
        }
        if (capacity==queue.size()){
            this.notifyAll();
        }
        return queue.remove();
    }


}
